﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class PhieuNhapXuatController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /PhieuNhapXuat/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.PhieuNhapXuats
                                select h;
            return View();
        }

        //
        // GET: /PhieuNhapXuat/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /PhieuNhapXuat/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten_dang_nhap.ToString()
            });

            IEnumerable<SelectListItem> listLoaiNhapXuat = db.LoaiNhapXuats.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString()+"("+c.Ki_hieu.ToString()+")"
            });

            IEnumerable<SelectListItem> listHTTT = db.HinhThucThanhToans.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListKhachHang"] = listKhachHang;
            ViewData["ListThanhVien"] = listThanhVien;
            ViewData["ListLoaiNhapXuat"] = listLoaiNhapXuat;
            ViewData["ListNgoaiTe"] = listNgoaiTe;
            ViewData["ListHTTT"] = listHTTT;
            return View();

        } 

        //
        // POST: /PhieuNhapXuat/Create

        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(PhieuNhapXuat phieunhapxuat)
        {
            try
            {
                // TODO: Add insert logic here
                phieunhapxuat.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.PhieuNhapXuats.InsertOnSubmit(phieunhapxuat);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten_dang_nhap.ToString()
                });

                IEnumerable<SelectListItem> listLoaiNhapXuat = db.LoaiNhapXuats.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
                });

                IEnumerable<SelectListItem> listHTTT = db.HinhThucThanhToans.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListKhachHang"] = listKhachHang;
                ViewData["ListThanhVien"] = listThanhVien;
                ViewData["ListLoaiNhapXuat"] = listLoaiNhapXuat;
                ViewData["ListNgoaiTe"] = listNgoaiTe;
                ViewData["ListHTTT"] = listHTTT;
                return View();
            }
        }
        
        //
        // GET: /PhieuNhapXuat/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            PhieuNhapXuat model = (from m in db.PhieuNhapXuats
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten_dang_nhap.ToString()
            });

            IEnumerable<SelectListItem> listLoaiNhapXuat = db.LoaiNhapXuats.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
            });

            IEnumerable<SelectListItem> listHTTT = db.HinhThucThanhToans.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListKhachHang"] = listKhachHang;
            ViewData["ListThanhVien"] = listThanhVien;
            ViewData["ListLoaiNhapXuat"] = listLoaiNhapXuat;
            ViewData["ListNgoaiTe"] = listNgoaiTe;
            ViewData["ListHTTT"] = listHTTT;
            return View(model);
        }

        //
        // POST: /PhieuNhapXuat/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, PhieuNhapXuat phieunhapxuat)
        {
            PhieuNhapXuat model = (from m in db.PhieuNhapXuats
                                   where m.ID == id
                                   select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ki_hieu = phieunhapxuat.Ki_hieu;
                model.Ma_khach_hang = phieunhapxuat.Ma_khach_hang;
                model.Ma_nhan_vien = phieunhapxuat.Ma_nhan_vien;
                model.Tong_tien = phieunhapxuat.Tong_tien;
                model.Thue = phieunhapxuat.Thue;
                model.Tien_thue = phieunhapxuat.Tien_thue;
                model.Loai_nhap_xuat = phieunhapxuat.Loai_nhap_xuat;
                model.Thanh_tien = phieunhapxuat.Thanh_tien;
                model.Ghi_chu = phieunhapxuat.Ghi_chu;
                model.Thanh_toan= phieunhapxuat.Thanh_toan;
                model.Chiet_khau=phieunhapxuat.Chiet_khau;
                model.Tien_chiet_khau=phieunhapxuat.Tien_chiet_khau;
                model.Ma_ngoai_te=phieunhapxuat.Ma_ngoai_te;
                model.Ty_gia=phieunhapxuat.Ty_gia;
                model.Tien_no = phieunhapxuat.Tien_no;
                model.Ma_hinh_thuc_thanh_toan = phieunhapxuat.Ma_hinh_thuc_thanh_toan;
                model.Xuat_hoa_don = phieunhapxuat.Xuat_hoa_don;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhachHang = db.KhachHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten_dang_nhap.ToString()
                });

                IEnumerable<SelectListItem> listLoaiNhapXuat = db.LoaiNhapXuats.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listNgoaiTe = db.NgoaiTes.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Don_vi_goc.ToString() + "(" + c.Ki_hieu.ToString() + ")"
                });

                IEnumerable<SelectListItem> listHTTT = db.HinhThucThanhToans.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListKhachHang"] = listKhachHang;
                ViewData["ListThanhVien"] = listThanhVien;
                ViewData["ListLoaiNhapXuat"] = listLoaiNhapXuat;
                ViewData["ListNgoaiTe"] = listNgoaiTe;
                ViewData["ListHTTT"] = listHTTT;
                return View(model);
            }
        }

        //
        // GET: /PhieuNhapXuat/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.PhieuNhapXuats
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.PhieuNhapXuats.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /PhieuNhapXuat/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
